/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nightonke.boommenusample.slice;

import com.nightonke.boommenu.BoomMenuButton;
import com.nightonke.boommenusample.BuilderManager;
import com.nightonke.boommenusample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class RecyclerViewAbilitySlice extends AbilitySlice {
    /**
     * onStart
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_recycler_view);

        ListContainer listView = (ListContainer) findComponentById(ResourceTable.Id_recycler_view);
        assert listView != null;
        DirectionalLayoutManager directionalLayoutManager = new DirectionalLayoutManager();
        directionalLayoutManager.setOrientation(Component.VERTICAL);
        listView.setLayoutManager(directionalLayoutManager);
        listView.setItemProvider(new RecyclerViewAdapter());
        listView.setItemClickedListener(
                new ListContainer.ItemClickedListener() {
                    @Override
                    public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                        BoomMenuButton bmb = (BoomMenuButton)component.findComponentById(ResourceTable.Id_bmb1);
                        bmb.boom();
                    }
                });
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    class RecyclerViewAdapter extends BaseItemProvider {

        @Override
        public int getCount() {
            return 1000;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public Component getComponent(int position, Component convertView, final ComponentContainer parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_item, null,true);

                viewHolder = new ViewHolder();
                viewHolder.text = (Text) convertView.findComponentById(ResourceTable.Id_text);
                viewHolder.bmb1 = (BoomMenuButton) convertView.findComponentById(ResourceTable.Id_bmb1);
                viewHolder.bmb2 = (BoomMenuButton) convertView.findComponentById(ResourceTable.Id_bmb2);
                viewHolder.bmb3 = (BoomMenuButton) convertView.findComponentById(ResourceTable.Id_bmb3);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.text.setText("No. " + position);

            viewHolder.bmb1.clearBuilders();
            for (int i = 0; i < viewHolder.bmb1.getPiecePlaceEnum().pieceNumber(); i++)
                viewHolder.bmb1.addBuilder(BuilderManager.getSimpleCircleButtonBuilder());
            viewHolder.bmb1.buildButton();

            viewHolder.bmb2.clearBuilders();
            for (int i = 0; i < viewHolder.bmb2.getPiecePlaceEnum().pieceNumber(); i++)
                viewHolder.bmb2.addBuilder(BuilderManager.getHamButtonBuilder());
            viewHolder.bmb2.buildButton();

            viewHolder.bmb3.clearBuilders();
            for (int i = 0; i < viewHolder.bmb3.getButtonPlaceEnum().buttonNumber(); i++)
                viewHolder.bmb3.addBuilder(BuilderManager.getSimpleCircleButtonBuilder());
            viewHolder.bmb3.buildButton();
            return convertView;
        }

        class ViewHolder {
            private Text text;
            private BoomMenuButton bmb1;
            private BoomMenuButton bmb2;
            private BoomMenuButton bmb3;
        }
    }


}
